#include "Fitting.h"
#include "Linequ.h"
#include "LS.h"
#include "Matrix.h"
#include <vector>
Fitting::Fitting(){

}

Fitting::~Fitting(){

}



vector<double> Fitting::getFitCurve()
{
	string file_x = "H:/pro_software_workplace/VisualStudio/Project2/Project2/pose_x2.txt";
	string file_y = "H:/pro_software_workplace/VisualStudio/Project2/Project2/pose_y2.txt";
	ifstream infile_x, infile_y;
	infile_x.open(file_x);
	infile_y.open(file_y);
	if (!infile_x && !infile_y)
		cout << "error" << endl;
	double temp;
	vector<double> Data_x, Data_y;
	while (infile_x >> temp)
	{
		Data_x.push_back(temp);
	}
	while (infile_y >> temp)
	{
		Data_y.push_back(temp);
	}

	
	vector<double> CoArr = fitCurve(Data_x, Data_y,2); 
	return CoArr;
}

vector<double> Fitting::getFitPoint(vector<double> coArr, vector<double> pointArr)
{
	vector<double> finalPoint;
	if (pointArr.size() == 0 || coArr.size() == 0)
	{
		cout << "error when reading getFitPoint" << endl;
	}
	if (pointArr.size() > 0 && coArr.size() > 0)
	{
		for (int i = 0; i < pointArr.size(); i++)
		{
			double temp = 0;
			for (int j = 0; j < coArr.size(); j++)
			{
				temp += pow(pointArr[i], j)*coArr[j];
			}
			finalPoint.push_back(temp);
		}
	}
	return finalPoint;
}

vector<double> Fitting::fitCurve(vector<double> arr_x, vector<double> arr_y, int n)
{
	CLS m_cls;
	vector<double> coefficientsSet;
	if (arr_x.size() != arr_y.size())
	{
		cout << " ������������" << endl;
	}
	if (arr_x.size() == arr_y.size())
	{
		for (int i = 0; i < arr_x.size(); i++)
		{
			m_cls.addPoint(arr_x[i], arr_y[i]);
		}
		m_cls.setN(n);
		m_cls.Solve();
		double *t_paracof = m_cls.getSolution();
		for (int i = 0; i < n + 1; i++)
		{
			coefficientsSet.push_back(t_paracof[i]); 
		}
	}
	return coefficientsSet;
}
